﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;

namespace AsyncPage
{
    public partial class LongRunning : System.Web.UI.Page
    {

        private bool Register;

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            if (Register)
            {
                AddOnPreRenderCompleteAsync(
                    new BeginEventHandler(BeginAsyncOperation),
                    new EndEventHandler(EndAsyncOperation)
                );
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
          

           
        }

        private IAsyncResult BeginAsyncOperation(object sender, EventArgs e,
            AsyncCallback cb, object state)
        {
            Action act = new Action(this.LongRunningOperation);
            return act.BeginInvoke(cb, state);
        }
        private void EndAsyncOperation(IAsyncResult ar)
        {
            Label1.Text = "end";
        }

        private void LongRunningOperation()
        {
            Thread.Sleep(5000);
        }


        protected void Button1_Click(object sender, EventArgs e)
        {
            Register = true;
        }
    }
}